package cn.cjx.basic.service.impl;

import cn.cjx.basic.mapper.BaseMapper;
import cn.cjx.basic.query.BaseQuery;
import cn.cjx.basic.service.IBaseService;
import cn.cjx.basic.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// @Service 在公共的service层实现类中不要加上此注解，因为此类只是给模块自己的实现类来使用的
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public QueryResult<T> queryPage(BaseQuery baseQuery) {
        // 1.查询分页总条数
        Long total = baseMapper.queryCount(baseQuery);

        // 2.查询分页数据
        List<T> rows = baseMapper.queryPage(baseQuery);


        return new QueryResult(total, rows);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }
}
